/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evolve.webbot.file;
import  java.util.ArrayList;
import  java.util.StringTokenizer;
import  java.io.IOException;
import  java.io.BufferedReader;
import  java.io.FileReader;
import  java.io.FileWriter;
import  java.io.PrintWriter;


import   com.evolve.webbot.bean.SorianaBean;
/**
 *
 * @author DarthAE
 */
public class TxtSoriana 
{
    private String filePath;

    public TxtSoriana(){}
    
    public TxtSoriana(String filePath)
    {
        this.filePath=filePath;
    } 
    
    public ArrayList<SorianaBean>  readSorianaTxtFile()
    {
      ArrayList<SorianaBean> sorianaRecordList= new ArrayList<SorianaBean>();  
      boolean inicioCant = false;  
            
      try {
             BufferedReader br = new BufferedReader(new FileReader(this.filePath));
             String linea = br.readLine();

             while (linea != null) 
             {

               if(linea.contains("TOTALES"))
               {
                   inicioCant = false;
               }    
               
               if(inicioCant)
               {  
                 sorianaRecordList.add(getValues(linea));
               }               
               
               if(linea.contains("Dimensión"))
               {
                    inicioCant = true; 
               }   
               
               linea = br.readLine();
             }
             
           } catch(IOException e){}
      
      return sorianaRecordList;
    }
    
    public SorianaBean getValues(String linea)
    {
        SorianaBean soriana = new SorianaBean();
        String  value   = null;
        int     k       = 0;
        
        StringTokenizer tokens = new StringTokenizer(linea);

             while(tokens.hasMoreTokens())
             {
                 value=tokens.nextToken().trim();
                 value=cleanValue(value);
                 
                 switch(k)
                 {
                     case 0: soriana.setDimension(value);
                             break;
                     case 1: soriana.setUnidadesVendidas(value);
                             break; 
                     case 2: soriana.setValorVentas(value);
                             break;  
                     case 3: soriana.setUnidadesCompradas(value);
                             break;     
                     case 4: soriana.setValorCompras(value);
                             break;
                     case 5: soriana.setExistencia(value);
                             break;   
                     default:System.out.println("no value");
                             break;
                 }                 
                 k++;
             }            
        
        return soriana;
    }
    
    public String cleanValue(String value)
    {
        String cleanValue = null;
        
        if(value!=null)
        {
             for(int i=0;i<127;i++)
             {   
               if(i!=46 && (i<=47 || i>=58))
               {
                 char n = (char)i;
                 value = value.replace(n,' ');
                 value = value.replaceAll(" ","");
               }                 
             }             
             cleanValue = value;       
        }
        
        return cleanValue;
    }
    
    public void writeSorianaDataFile(ArrayList<SorianaBean> sorianaRecordList)
    {
        try {
               FileWriter  outFile = new FileWriter(this.filePath.replaceAll("vyc","data"));
               PrintWriter out     = new PrintWriter(outFile);
              
               for(int i=0;i<sorianaRecordList.size();i++)
               {
                 out.println(sorianaRecordList.get(i).toString());
               }
              
               out.close();
               
            } catch (IOException e){e.printStackTrace();}
    }
    
    public static void main(String args[])
    {
        ArrayList<SorianaBean> sorianaRecordList = new ArrayList<SorianaBean>();
        TxtSoriana txtSoriana                = new TxtSoriana("F:\\evolve-webbot\\soriana-webbot\\soriana-files\\Soriana-vyc-14082012-213236-mensual.txt");
        
        sorianaRecordList = txtSoriana.readSorianaTxtFile();
        txtSoriana.writeSorianaDataFile(sorianaRecordList);
    }    
}
